/**
 * 
 */
var commendingConfig = {
    icon:'',
    width:'50px',
    height:'80px',
    top:'160px',
    right:'5%',
    posterUrl:'',
    text:'',
    textVisible:true
};
if (document.currentScript) {
    commendingConfig.icon = document.currentScript.getAttribute("icon")||commendingConfig.icon;
    commendingConfig.width = document.currentScript.getAttribute("w")||commendingConfig.width;
    commendingConfig.height = document.currentScript.getAttribute("h")||commendingConfig.height;
    commendingConfig.top = document.currentScript.getAttribute("t")||commendingConfig.top;
    commendingConfig.right = document.currentScript.getAttribute("r")||commendingConfig.right;
    commendingConfig.posterUrl = document.currentScript.getAttribute("posterUrl")||commendingConfig.posterUrl;
    commendingConfig.text = document.currentScript.getAttribute("text")|| commendingConfig.text;
    commendingConfig.textVisible = document.currentScript.getAttribute("textvisible")!=="false";
}

var _poster_initial =function()
{ 
    var poster_box  = document.createElement('div');
     
    poster_box.style.position="fixed";
    poster_box.style.display = "flex";
    poster_box.style.flexFlow = "column";
    poster_box.style.justifyContent = "center";
    poster_box.style.alignItems = "center";
    poster_box.style.width = commendingConfig.width;
    poster_box.style.height = commendingConfig.height;
    poster_box.style.top = commendingConfig.top;
    poster_box.style.right = commendingConfig.right;
    poster_box.style.lineHeight = "30px";
    poster_box.style.fontSize = "80%";
    poster_box.style.cursor = "pointer";
    poster_box.style.backgroundColor = "transparent"; 

    var img  = document.createElement('img'); 
    img.style.border = "1px #a8a7a7 solid"; 
    img.style.width = "50px";
    img.style.height = "50px"; 
    img.style.borderRadius = "5px";
    img.style.cursor = "pointer"; 
    img.style.display = "block"; 
    img.style.backgroundColor = "transparent"; 
    if(commendingConfig.icon.length>0)
    {
        img.style.backgroundImage = "url("+commendingConfig.icon+")";
    } 
    img.style.backgroundPosition = "center";
    img.style.backgroundSize = "70%";
    img.style.backgroundRepeat = "no-repeat"; 
 

    var btn = document.createElement('label');
    btn.title="关闭";
    btn.style.height = "50px";
    btn.style.width = "50px";
    btn.style.backgroundImage = "url("+Rsd.getRedjsUrl("/app/resources/svg/close.svg")+")";
    btn.style.backgroundSize = "60%";
    btn.style.backgroundPosition = "center";
    btn.style.backgroundRepeat = "no-repeat"; 
    btn.style.cursor = "pointer";
    btn.style.display = "none";
    btn.style.float = "right";
    btn.style.color = "#fff";
    btn.style.position = "relative";
    btn.style.top = "0px";
    btn.style.right = "5px";
    
    var txt = document.createElement('label');
    txt.innerHTML = "长按下载图片，双击关闭";
    txt.style.color = "#524c4c9e";
    //txt.style.fontSize= "80%";
    txt.style.display = "none";
    txt.style.color = "#fff";
    txt.style.position = "relative";
    txt.style.cursor = "pointer";
    txt.style.lineHeight = "80px";
    txt.style.textAlign = "center";

    poster_box.appendChild(btn);
    poster_box.appendChild(img);

    if(commendingConfig.textVisible)
    {
        var label = document.createElement('label');
        label.innerHTML = commendingConfig.text;
        label.style.color = "#524c4c9e";
        label.style.fontSize= "80%"; 
        label.style.bottom = "0px";
        label.style.width = "100%";
        label.style.textAlign = "center";
         
        poster_box.appendChild(label);
    }
 
    poster_box.appendChild(txt);
    document.body.appendChild(poster_box);
    
    var closePoster = function()
    {
        poster_box.style.zIndex=0;
        poster_box.style.width = commendingConfig.width;
        poster_box.style.height = commendingConfig.height;
        poster_box.style.top = commendingConfig.top;
        poster_box.style.right = commendingConfig.right;

        poster_box.style.borderRadius = "5px"; 
        poster_box.style.backgroundColor = "transparent";
        poster_box.style.cursor = "pointer";
        poster_box.style.border = "";
        poster_box.style.boxShadow= "";
        
        img.src = "";
        img.style.width = "50px";
        img.style.height= "50px";
        img.style.maxWidth = "unset"; 
        img.style.borderRadius = "5px";
        img.style.border = "1px #a8a7a7 solid";
        txt.style.display = "none";
        label.style.display = "";
        btn.style.display ="none";
    };
    var showPoster = function()
    {
        poster_box.style.zIndex=999;
        poster_box.style.top = "0px";
        poster_box.style.right = "0px"
        poster_box.style.width = "100%";
        poster_box.style.height = "100%";  
        poster_box.style.backgroundColor = "#01010173";
        poster_box.style.cursor = "default"; 
        
        img.style.width = "";
        img.style.flex = 1;
        img.style.maxWidth = "95%"; 
        img.style.overflow = "hidden";
        img.style.borderRadius = "10px";
        img.style.border = "";
        if(commendingConfig.posterUrl.length > 0)
        {
            img.src = commendingConfig.posterUrl; 
        }
        txt.style.display = "";
        label.style.display = "none";
        btn.style.display="";
    };
    //双击关闭事件
    img.ondblclick =function(e)
    {    
        closePoster();
        e.stopPropagation();
    };
     
    poster_box.onclick = function(e)
    {  
        if(poster_box.style.width == "100%")
        {
            if(e.target == img)
            {
                e.stopPropagation();
                return;
            }
            closePoster();
        }
        else
        {
            showPoster();
        }
        
        e.stopPropagation();
    }
};
window.addEventListener('load',_poster_initial);